<template>
  <div>
      <van-cell title="搜索历史">
          <div v-if="isDeleteShow">
            <!-- 方式1.不可以,数组/对象赋值改变指向,会被vue发现 -->
            <!-- <span @click="searchHistories = []">全部删除</span> -->
            <!-- 方式2.通过数组方法删除所有 -->
            <!-- <span @click="searchHistories.splice(0)">全部删除</span> -->
            <!-- 方式3.通过父子通信 -->
            <span @click="$emit('clear-search-histories')">全部删除</span>
            &nbsp;&nbsp;
            <span @click="isDeleteShow = false">完成</span>
          </div>
          <van-icon v-else name="delete-o" @click="isDeleteShow = true"/>
      </van-cell>
      <van-cell :title="item" v-for="(item, index) in searchHistories" :key="index" @click="onItemClick(item, index)">
        <van-icon v-show="isDeleteShow" name="close"></van-icon>
      </van-cell>
  </div>
</template>

<script>
export default {
  name: 'SearchHistory',
  props: {
    searchHistories: {
      type: Array,
      required: true
    }
  },
  components: {},
  data () {
    return {
      isDeleteShow: false // 是否是删除状态
    }
  },
  created () {

  },
  methods: {
    onItemClick (item, index) {
      if (this.isDeleteShow) {
        // 删除操作
        this.searchHistories.splice(index, 1)
      } else {
        // 搜索操作
        this.$emit('search', item)
      }
    }
  }
}
</script>

<style lang="less" scoped>

</style>
